//
//  YKEmotionListView.m
//  youkan
//
//  Created by 吴高烽 on 16/4/14.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKEmotionListView.h"
#import "YKEmotinoPageView.h"
@interface YKEmotionListView()<UIScrollViewDelegate>
@property (nonatomic ,weak)UIScrollView *scrollView;
@property (nonatomic ,weak)UIPageControl *pageControl;
@end
@implementation YKEmotionListView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self setUpAllChilen];
    }
    return self;
}
-(void)setUpAllChilen{
    UIScrollView *scrollViwe=[[UIScrollView alloc]init];
    scrollViwe.pagingEnabled =YES;
    scrollViwe.delegate =self;
//    scrollViwe.alwaysBounceVertical =NO;
    //    去除水平方向的滚动条
    scrollViwe.showsHorizontalScrollIndicator=NO;
    //    去除竖直方向的滚动条
    scrollViwe.showsVerticalScrollIndicator =NO;
    [self addSubview:scrollViwe];
    self.scrollView =scrollViwe;
    UIPageControl *pageControl =[[UIPageControl alloc]init];
    pageControl.userInteractionEnabled =NO;
    pageControl.backgroundColor =[UIColor whiteColor];
//    图片是平铺的，有瑕疵
//    pageControl.currentPageIndicatorTintColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_keyboard_dot_selected"]];
//    pageControl.pageIndicatorTintColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_keyboard_dot_normal"]];
    [pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKey:@"pageImage"];
    [pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKey:@"currentPageImage"];
    pageControl.currentPage =0;
    [self addSubview:pageControl];
    self.pageControl =pageControl;
}
-(void)setEmotions:(NSArray *)emotions{
    _emotions =emotions;
//       1.设置页数
    self.pageControl.numberOfPages =(emotions.count+YKEmotionPageSize-1)/YKEmotionPageSize;
           NSRange range;
//       2. 显示每一页表情的控件
    NSInteger count =self.pageControl.numberOfPages;
    for (NSInteger i=0; i<count; i++) {
        YKEmotinoPageView *pageView=[[YKEmotinoPageView alloc]init];
        range.location = i* YKEmotionPageSize;
        range.length = YKEmotionPageSize;
        NSUInteger left =emotions.count - i * YKEmotionPageSize;
        if (left>=YKEmotionPageSize) {
            range.length =YKEmotionPageSize;
        }else{
            range.length =left;
        }
//        if (i==count-1) {
//            range.length =self.emotions.count-range.location;
//        }
        
        pageView.emotions =[self.emotions subarrayWithRange:range];
        [self.scrollView addSubview:pageView];
    }


}

// 统一设置控件的frame
-(void)layoutSubviews{
    [super layoutSubviews];
    self.pageControl.width =self.width;
    self.pageControl.height =35;
    self.pageControl.y=self.height-self.pageControl.height;
    self.pageControl.x=0;
    
    self.scrollView.width =self.width;
    self.scrollView.height =self.pageControl.y;
    self.scrollView.x =0;
    self.scrollView.y=0;
    //       2. 显示每一页表情的控件
    NSUInteger count = self.scrollView.subviews.count;
    for (NSInteger i=0; i<count; i++) {
        YKEmotinoPageView *pageView=self.scrollView.subviews[i];
        pageView.height = self.scrollView.height ;
        pageView.width = self.scrollView.width;
        pageView.x = pageView.width*i;
        pageView.y = 0;
    }
    //    3.设置scrollView的contentSize
    self.scrollView.contentSize =CGSizeMake(count*SCWidth, 0);
//    YKLog(@"%@",self.scrollView.subviews);
}
#pragma mark -UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    double pageNo =scrollView.contentOffset.x/scrollView.width;
    self.pageControl.currentPage =(int)(pageNo+0.5);
}
@end
